package org.brisling.common;

import java.awt.image.BufferedImage;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.brisling.sysman.domain.Department;
import org.brisling.sysman.domain.Permissions;
import org.brisling.sysman.domain.SysDictionary;
import org.brisling.sysman.domain.Users;
import org.brisling.sysman.domain.VODepartment;
import org.brisling.sysman.domain.VOJobUser;
import org.springframework.stereotype.Component;

/**
 * 系统初始化参数配置加载组件
 * @author jacksonWang
 *
 */
@Component
public class SysConfig  {
	
	
	//系统参数配置map
	private static HashMap<String,String> maps = null;
	
	private static HashMap<String,Users> userMap = new HashMap<String,Users>();
	
	private static HashMap<Long,Department> deptMap = new HashMap<Long,Department>();
	
	//部门扩展信息视图map
	private static HashMap<Long,VODepartment> deptExtendMap = new HashMap<Long,VODepartment>();
	
	//权限刷新时间点
	private static Calendar permissionRefreshCalendar= Calendar.getInstance();
	
	//用户权限缓存
	private static HashMap<Long,HashMap<String,List<Permissions>>> userPermissionMap = new HashMap<Long,HashMap<String,List<Permissions>>>();
	
	//用户菜单缓存
	private static HashMap<Long,HashMap<String,List<Permissions>>> userMenuPermissionMap = new HashMap<Long,HashMap<String,List<Permissions>>>();
	
	//字典树
	private static HashMap<Long,List<SysDictionary>> dicTreeMap = new HashMap<Long,List<SysDictionary>>();
	
	//字典sql定义
	private static HashMap<String,String> bizidSqlMap = new HashMap<String,String>();
	
	//定义流程业务map对象
	private static Map<String,Permissions> flowBizMap = new HashMap<String,Permissions>();
	
	
	//定义流程业务map对象
	private static Map<String,Permissions> flowBizcodeMap = new HashMap<String,Permissions>();
	
	//管理用户缓存
	private static Map<Long,Map<Long,List<VOJobUser>>> manageUserMap = new HashMap<Long,Map<Long,List<VOJobUser>>>();
	
	//启用日志
	private static Boolean enableLog = true;
	
	public static Calendar getPermissionRefreshCalendar() {
		return permissionRefreshCalendar;
	}

	public static void setPermissionRefreshCalendar() {
		SysConfig.permissionRefreshCalendar = Calendar.getInstance();
		userPermissionMap = new HashMap<Long,HashMap<String,List<Permissions>>>();
		userMenuPermissionMap = new HashMap<Long,HashMap<String,List<Permissions>>>();
	}
	private static HashMap<Long,BufferedImage> slideImage = new HashMap<Long,BufferedImage>();
//	private static Users usr = null;
	
	//流程节点配置map
	private static HashMap<String,Object> flowNodeMaps = null;
	
	//测试用户配置map
	private static HashMap<String,String> testUserMaps = null;
	
	
	
	//缓存流程节点流向
	private static Map<String,Map<String,List<String[]>>> outgoingMap = null;
	
	
		
	private static SysConfig sysConfig ;
	
//	private static String hospital_name = null ;
	
	private SysConfig(){}
	
	public static SysConfig getSysConfig(){
		if(sysConfig==null)
			sysConfig = new SysConfig();
		return sysConfig;
	}
	/*
	 * 应用sso登录
	 */
	private static Boolean usingSSO =false;	
	
	

	public static Boolean  getUsingSSO() {		
		String sso = getConfig("usingSSO");
		if(sso!=null && sso.trim().length()>0){
			sso=sso.trim().toLowerCase();
			if(sso.compareTo("true")==0 ||(sso.compareTo("1")==0)){
				usingSSO = true;
			}
		}
		
		return usingSSO;
	}

	public static void setUsingSSO(Boolean usingSSO) {
		getSysConfig();
		SysConfig.usingSSO = usingSSO;
	}
	
	
	
	public static String getConfig(String configName){
		
		getSysConfig();
		
		String retVal = null;
		if(configName !=null && configName.trim().length()>0){
			configName = configName.trim();
			
			if(maps!=null&&(maps.containsKey(configName))){
				retVal = maps.get(configName);
				if(retVal!=null && retVal.trim().length()>0){
					retVal = StringUtils.normalizeSpace(retVal);
				}
			}
		}
		return retVal;
	}

	public static HashMap<String, String> getMaps() {
		getSysConfig();
		return maps;
	}

	public static void setMaps(HashMap<String, String> maps) {
		getSysConfig();
		SysConfig.maps = maps;
		initTestUserMap();
	}
	
	
	public static boolean isTestUser(String userid){
		getSysConfig();
		boolean retVal = false;
		if(userid==null || userid.trim().length()<=0){
			return retVal;
		}
		
		if((testUserMaps == null) || (testUserMaps.isEmpty())){
			
			initTestUserMap();
		}
		
		if(testUserMaps.containsKey(userid)){
			retVal = true;
		}
		
		return retVal;
		
	}
	
	/*
	 * 初始化测试用户map 
	 */
	private static void initTestUserMap(){		
		if(maps.containsKey("test_users")){
			testUserMaps = new HashMap<String,String>();
			String userids = maps.get("test_users");
			if(userids!=null && userids.length()>0){
				String[] ids = userids.split(",");
				if(ids!=null && (ids.length>0)){
					for(int i=0;i<ids.length;i++){
						testUserMaps.put(ids[i], ids[i]);
					}
				}
			}
		}
	}

	/**
	 * 获取流程定义节点map
	 * @param activitiId	流程节点定义id
	 * @param fullBizcode	流程业务完整编码
	 * @return
	 */
	public static Map<String,Map<String,String>> getFlowNodeMaps(String activitiId,String fullBizcode) {
		getSysConfig();
		if(activitiId!=null && fullBizcode!=null){
			String _key = activitiId+fullBizcode;
			if(flowNodeMaps!=null && flowNodeMaps.containsKey(_key)){
				Object obj = flowNodeMaps.get(_key);
				if(obj!=null){
					Map<String,Map<String,String>> map = (Map<String,Map<String,String>>)obj;
					return map;
				}
			}
		}
		return null;
	}
	
	/**
	 * 获取流程定义节点map
	 * @return
	 */
	public static HashMap<String, Object> getFlowNodeMaps() {
		getSysConfig();
		return flowNodeMaps;
	}

	public static void setFlowNodeMaps(HashMap<String, Object> flowNodeMaps) {
		getSysConfig();
		SysConfig.flowNodeMaps = flowNodeMaps;
	}

	/**
	 * 根据用户唯一编码获取用户信息
	 * @param idendify
	 * @return
	 */
	public static Users getUsr(String idendify) {
		getSysConfig();
		if(userMap.containsKey(idendify))
			return userMap.get(idendify);
		else
			return null;
	}

	/**
	 * 设置用户信息到用户map（缓存）
	 * @param usr
	 */
	public static void setUsr(Users usr) {
		getSysConfig();
		
		if(usr!=null && usr.getId()!=null){		
			userMap.put(usr.getId().toString(), usr);
		}
	}
	
	/**
	 * 清除用户缓存
	 */
	public static void clearUserCache(){
		getSysConfig();
		userMap  = new HashMap<String,Users>();
	}

	public static Department getDept(Long idendify) {
		getSysConfig();
		if(deptMap.containsKey(idendify))
			return deptMap.get(idendify);
		else
			return null;
	}

	public static void setDept(Department dept) {
		getSysConfig();
		if(dept!=null && dept.getId()!=null){		
			deptMap.put(dept.getId(), dept);
		}		
	}

	public static String getHospital_name() {
		getSysConfig();
		return SysConfig.getConfig("hospital_name");
	}

	public static BufferedImage getSlideImage(Long id) {
		getSysConfig();
		if(slideImage.containsKey(id)){
			return slideImage.get(id);
		}
		return null;
	}

	public static void setSlideImage(Long id,BufferedImage contents) {
		getSysConfig();
		if(id!=null && contents!=null){
			slideImage.put(id, contents);
		}		
	}
	
	
	//缓存用户权限
	public static void setUserPermissions(Long userid,String bizcode,List<Permissions> permissionList){
		getSysConfig();
		if(userid!=null && (bizcode!=null && bizcode.trim().length()>0) && (permissionList!=null && permissionList.size()>0)){
			String _bizcode = bizcode.trim();
			HashMap<String,List<Permissions>> pMap = null;
			if(userPermissionMap.containsKey(userid)){
				pMap = userPermissionMap.get(userid);
			}else{
				pMap = new HashMap<String,List<Permissions>>();
			}
			pMap.put(_bizcode, permissionList);
			
			userPermissionMap.put(userid, pMap);
		}
	}
	
	
	public static List<Permissions> getUserPermissions(Long uid,String bizcode){
		if(uid!=null && (bizcode!=null && bizcode.trim().length()>0)){
			String _bizcode = bizcode.trim();
			
			if(userPermissionMap.containsKey(uid)){
				HashMap<String,List<Permissions>> pMap = userPermissionMap.get(uid);
				
				if(pMap!=null && pMap.containsKey(_bizcode)){
					return pMap.get(_bizcode);
				}
			}
		}
		//参数异常或无权限，返回空值
		return null;
	}

	/**
	 * 获取用户站点菜单权限
	 * @param uid
	 * @param siteName
	 * @return
	 */
	public static List<Permissions> getUserMenuPermissionMap(Long uid,String siteName) {
		if(uid!=null && (siteName!=null && siteName.trim().length()>0)){
			String _bizcode = siteName.trim();
			
			if(userMenuPermissionMap.containsKey(uid)){
				HashMap<String,List<Permissions>> pMap = userMenuPermissionMap.get(uid);
				
				if(pMap!=null && pMap.containsKey(_bizcode)){
					return pMap.get(_bizcode);
				}
			}
		}
		//参数异常或无权限，返回空值
		return null;
	}

	/**
	 * 设置用户站点菜单权限
	 * @param userid
	 * @param siteName
	 * @param permissionList
	 */
	public static void setUserMenuPermissionMap(Long userid,String siteName,List<Permissions> permissionList){
		getSysConfig();
		if(userid!=null && (siteName!=null && siteName.trim().length()>0) && (permissionList!=null && permissionList.size()>0)){
			String _bizcode = siteName.trim();
			HashMap<String,List<Permissions>> pMap = null;
			if(userMenuPermissionMap.containsKey(userid)){
				pMap = userMenuPermissionMap.get(userid);
			}else{
				pMap = new HashMap<String,List<Permissions>>();
			}
			pMap.put(_bizcode, permissionList);
			
			userMenuPermissionMap.put(userid, pMap);
		}
	}

	
	public static void refreshDicParam(){
		dicTreeMap = new HashMap<Long, List<SysDictionary>> ();
		bizidSqlMap = new HashMap<String, String>();
	}
	
	public static  List<SysDictionary> getDicTree(Long id) {
		
		if(id==null){
			id = -1l;
		}
		
		if(dicTreeMap.containsKey(id)){
			return dicTreeMap.get(id);
		}
		
		return null;
	}

	public static void setDicTree(Long id, List<SysDictionary> diclist) {
		if(id==null){
			id = -1l;
		}
		if(diclist!=null && diclist.size()>0){
			dicTreeMap.put(id, diclist);
		}
	}

	public static String getBizidSql(String bizid) {
		if(bizid!=null && bizid.trim().length()>0){
			bizid = bizid.trim();
			if(bizidSqlMap.containsKey(bizid)){
				return bizidSqlMap.get(bizid);
			}
		}
		return null;
	}

	public static void setBizidSql(String bizid,String sql) {
		
		if((bizid!=null && bizid.trim().length()>0 )&& (sql!=null && sql.trim().length()>0)){
			bizid = bizid.trim();
			bizidSqlMap.put(bizid, sql);
		}		
	}

	public static HashMap<Long, Department> getDeptMap() {
		getSysConfig();
		return deptMap;
	}

	public static void setDeptMap(HashMap<Long, Department> deptMap) {
		getSysConfig();
		SysConfig.deptMap = deptMap;
	}

	public static Map<String, Permissions> getFlowBizMap() {
		getSysConfig();
		return flowBizMap;
	}

	public static void setFlowBizMap(Map<String, Permissions> flowBizMap) {
		getSysConfig();
		SysConfig.flowBizMap = flowBizMap;
	}

	public static HashMap<Long, VODepartment> getDeptExtendMap() {
		getSysConfig();
		return deptExtendMap;
	}

	public static void setDeptExtendMap(HashMap<Long, VODepartment> deptExtendMap) {
		getSysConfig();
		SysConfig.deptExtendMap = deptExtendMap;
	}

	//获取指定部门的主管领导id
	public static String getDeptLeaderIds (Long deptid){
		getSysConfig();
		if(deptid!=null ){
			if(deptExtendMap!=null && deptExtendMap.containsKey(deptid)){
				return deptExtendMap.get(deptid).getLeader_ids();
			}
		}
		return null;
	}
	
	/**
	 * 获取业务主管领导id值
	 * @param fullBizcode
	 * @return
	 */
	public static String getBizLeaderIds(String fullBizcode){
		getSysConfig();
		if(fullBizcode!=null ){
			if(flowBizMap!=null && flowBizMap.containsKey(fullBizcode)){
				return flowBizMap.get(fullBizcode).getLeader_ids();
			}
		}
		return null;
	}

	public static Map<Long, Map<Long, List<VOJobUser>>> getManageUserMap() {
		getSysConfig();
		return manageUserMap;
	}

	public static void setManageUserMap(Map<Long, Map<Long, List<VOJobUser>>> manageUserMap) {
		getSysConfig();
		SysConfig.manageUserMap = manageUserMap;
	}
	
	/**
	 * 按照岗位编码和部门编码获取管理用户id
	 * @param jobcode
	 * @param deptid
	 */
	public static List<VOJobUser> getManageUserByJobcodeAndDept(Long jobcode,Long deptid){
		getSysConfig();
		
		
		List<VOJobUser> retList = null;
		//判断传入参数是否为空
		if(jobcode!=null && deptid!=null){
			
			//判断管理用户集合是否为空
			if(manageUserMap!=null){
				
				//岗位集合是否存在
				if(manageUserMap.containsKey(jobcode)){
					Map<Long, List<VOJobUser>> deptMap = manageUserMap.get(jobcode);
					
					//科室集合是否存在
					if(deptMap!=null && deptMap.containsKey(deptid)){
						List<VOJobUser> lists=deptMap.get(deptid);
						
						if(lists!=null && lists.size()>0){
							retList = lists;
						}
					}
				}
				
				//当前部门未找到管理岗位人员
				if(retList ==null){
					Department d = deptMap.get(deptid);
					if(d!=null){
						Long _id = d.getParentid();
						
						if(_id!=null){
							retList = getManageUserByJobcodeAndDept(jobcode,_id);
						}
					}
				}
			}
		}
		
		
		
		return retList;
	}
	
	/**
	 * 获取指定部门岗位角色集合
	 * @param jobcodeArr
	 * @param deptid
	 * @return
	 */
	public static List<VOJobUser> getJobUserByDept(String jobcodeArr,String deptid){
		
		List<VOJobUser> lists = new ArrayList<VOJobUser>();
		if(jobcodeArr!=null && deptid!=null){
			String[] codeArr = jobcodeArr.split(",");
			Long _deptid = null;
			try{
				_deptid = Long.parseLong(deptid);
			}catch(Exception e){
				
			}
			
			if(_deptid!=null){
				for(String a:codeArr){
					Long codeLong = null;
					try{
						codeLong = Long.parseLong(a);
					}catch(Exception e){
						
					}
					
					if(codeLong!=null){
						List<VOJobUser> tlists = getManageUserByJobcodeAndDept(codeLong,_deptid);
						if(tlists!=null)
							lists.addAll(tlists);						
					}
				}
			}
		}
		
		return lists;
		
		
	}

	//获取和设置按照业务代码缓存的业务模块配置map
	public static Map<String, Permissions> getFlowBizcodeMap() {
		getSysConfig();
		return flowBizcodeMap;
	}

	public static void setFlowBizcodeMap(Map<String, Permissions> flowBizcodeMap) {
		getSysConfig();
		SysConfig.flowBizcodeMap = flowBizcodeMap;
	}

	
	/**
	 * 获取流程节点下一环节流向
	 * @param taskKey
	 * @param activityId
	 * @return
	 */
	public static List<String[]> getOutgoingMap(String taskKey,String activityId) {
		getSysConfig();		
		if( (outgoingMap !=null) && (outgoingMap.containsKey(activityId)) ){
			//业务模块流向配置存在
			Map<String,List<String[]>> keyConfig= outgoingMap.get(activityId);
			
			if(keyConfig!=null && keyConfig.containsKey(taskKey)){
				//节点流向配置存在
				return keyConfig.get(taskKey);
			}
		}
		return null;
	}

	public static Map<String,Map<String,List<String[]>>> getOutgoingMap(){
		getSysConfig();
		if(outgoingMap==null)
			outgoingMap = new HashMap<String,Map<String,List<String[]>>>();
		return outgoingMap;
	}
	/**
	 * 设置流程节点下一环节流向
	 * @param outgoingMap
	 */
	public static void setOutgoingMap(Map<String, Map<String, List<String[]>>> outgoingMap) {
		SysConfig.outgoingMap = outgoingMap;
	}

	public static Boolean getEnableLog() {
		getSysConfig();
		return enableLog;
	}

	public static void setEnableLog(Boolean enableLog) {
		getSysConfig();
		SysConfig.enableLog = enableLog;
	}

	

	
	
	
}
